package Controller;

import Service.UserService;
import com.github.pagehelper.PageInfo;
import domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVO userVO){

        PageInfo pageInfo = userService.findAllUserByPage(userVO);
        ResponseResult result = new ResponseResult(true, 200, "分页查询成功", pageInfo);

        return result;

    }

    /*
         用户登录
     */
                            // 接收前台传递的user对象
    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {

        User login = userService.login(user);
        if(login != null){

            // 保存用户id 以及 access_token到session中(HttpServletRequest)
            HttpSession session = request.getSession();
            String access_token = UUID.randomUUID().toString();
            session.setAttribute("access_token",access_token);
            session.setAttribute("user_id",login.getId());

            // 将查询出来的信息 响应给前台
            HashMap<String, Object> map = new HashMap<>();
            map.put("access_token",access_token);
            map.put("user_id",login.getId());

            // 将查询出的login对象也存放到map中
            map.put("user",login);

            return new ResponseResult(true,1,"登录成功",map);


        }else{
            return new ResponseResult(true,400,"该用户不存在",null);
        }
    }


    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(int userid){

        List<Role> list = userService.findUserRelationRoleById(userid);

        return new ResponseResult(true,200,"分配角色成功",list);
    }

    /*
       更改用户角色
      根据用户清空关联中间表 + 重新添加中间关联表
    */
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVO userVO){

        userService.userContextRole(userVO);

        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",userVO.getUserId());
        map.put("roleIdList",userVO.getRoleIdList());
        return new ResponseResult(true,200,"分配角色成功",null);
    }

    /*
        获取用户拥有的菜单权限
     */
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request){

//        1. 获取请求头中的token getHeader("Authorization",xxxxxx)
        String header_token = request.getHeader("Authorization");

        // 2.获取session中的token session.getAttribute("access_token")
        String access_token = (String) request.getSession().getAttribute("access_token");

        // 3.  判断两次取出的token是否一致
        if(header_token.equals(access_token)){
            //1. 获取用户ID
            Integer user_id = (Integer) request.getSession().getAttribute("user_id");
            ResponseResult userPermissions = userService.getUserPermissions(user_id);
            return userPermissions;

        }else {
            return new ResponseResult(false,400,"获取菜单信息失败 ",null);
        }


    }


}
